// index.js
const httpUtils = require('../../utils/httpUtils')
const ui = require('../../utils/ui')
import * as echarts from '../../ec-canvas/echarts';

Page({
  data: {
    str: null,
    id: '',
    userObj: {
      name: 1,
      age: 18
    },
    ec: {
      lazyLoad: true,
    },
  },

  onLoad(option) {
    let value = 'userObj.name';
    this.setData({
      id: option.id,
      [value]: '123',
      userObj: {
        a: 456
      }
    })
    console.log(this.data.id);
    this.getNetInfo()
    this.echartsComponnet = this.selectComponent('#mychart');
    this.init_echarts()
  },

  //获取接口数据
  getNetInfo() {
    let obj = {
      method: "POST",
      showLoading: true,
      url: `/user/register?username=pppooo11&password=pppooo&repassword=pppooo`,
      message: "正在注册..."
    }
    httpUtils.request(obj).then(res => {
      console.log(res);
      let obj = {
        a: 8888,
        b: 6666
      }
      this.setData({
        str: JSON.stringify(res),
        userObj: obj
      })
      console.log(this.data.userObj);
      console.log(this.data.id);
      ui.showToast(res.data.errorMsg)
    }).catch(err => {
      console.log('ERROR')
    });
  },



  init_echarts() {
    this.echartsComponnet.init((canvas, width, height) => {
      // 初始化图表
      const Chart = echarts.init(canvas, null, {
        width: width,
        height: height
      });
      Chart.setOption(this.getOption());
      // 注意这里一定要返回 chart 实例，否则会影响事件处理等
      return Chart;
    });
  },
  getOption() {
    let option22 = {
      xAxis: {
        type: 'category',
        data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
      },
      yAxis: {
        type: 'value'
      },
      series: [{
        data: [820, 932, 901, 934, 1290, 1330, 1320],
        type: 'line',
        smooth: true
      }]
    }
    return option22
  }
})